package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.shop.domain.ErpSellerStorePromiseTemplate;
import com.erp.shop.domain.bo.ErpSellerStorePromiseTemplateBo;
import com.erp.shop.domain.dto.ErpSellerStorePromiseTemplateDto;
import com.erp.shop.domain.vo.ErpSellerStorePromiseTemplateVo;
import com.erp.shop.mapper.ErpSellerStorePromiseTemplateMapper;
import com.erp.shop.service.IErpSellerStorePromiseTemplateService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 店铺服务模板Service业务层处理
 *
 * @author erp
 * @date 2023-03-29
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStorePromiseTemplateServiceImpl implements IErpSellerStorePromiseTemplateService {

    private final ErpSellerStorePromiseTemplateMapper baseMapper;

    /**
     * 查询店铺服务模板
     */
    @Override
    public ErpSellerStorePromiseTemplateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺服务模板列表
     */
    @Override
    public TableDataInfo<ErpSellerStorePromiseTemplateVo> queryPageList(ErpSellerStorePromiseTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStorePromiseTemplate> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStorePromiseTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺服务模板列表
     */
    @Override
    public List<ErpSellerStorePromiseTemplateVo> queryList(ErpSellerStorePromiseTemplateBo bo) {
        LambdaQueryWrapper<ErpSellerStorePromiseTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStorePromiseTemplate> buildQueryWrapper(ErpSellerStorePromiseTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStorePromiseTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSellerStoreId() != null, ErpSellerStorePromiseTemplate::getSellerStoreId, bo.getSellerStoreId());
        lqw.eq(bo.getTemplateId() != null, ErpSellerStorePromiseTemplate::getTemplateId, bo.getTemplateId());
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), ErpSellerStorePromiseTemplate::getTemplateName, bo.getTemplateName());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), ErpSellerStorePromiseTemplate::getIsDefault, bo.getIsDefault());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStorePromiseTemplate::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增店铺服务模板
     */
    @Override
    public Boolean insertByBo(ErpSellerStorePromiseTemplateBo bo) {
        ErpSellerStorePromiseTemplate add = BeanUtil.toBean(bo, ErpSellerStorePromiseTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺服务模板
     */
    @Override
    public Boolean updateByBo(ErpSellerStorePromiseTemplateBo bo) {
        ErpSellerStorePromiseTemplate update = BeanUtil.toBean(bo, ErpSellerStorePromiseTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStorePromiseTemplate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺服务模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean addPromiseTemplate(List<ErpSellerStorePromiseTemplateDto> templateDtoList, Long sellerId) {
        List<ErpSellerStorePromiseTemplate> templates = new ArrayList<>();
        ErpSellerStorePromiseTemplate template = new ErpSellerStorePromiseTemplate();
//        ErpSellerStorePromiseTemplate templateSel = new ErpSellerStorePromiseTemplate();
        //清空该店铺的所有服务模板，重新插入
        baseMapper.delete(new LambdaQueryWrapper<ErpSellerStorePromiseTemplate>()
                .eq(ErpSellerStorePromiseTemplate::getSellerStoreId,sellerId));
        for (ErpSellerStorePromiseTemplateDto templateDto: templateDtoList) {
            template.setTemplateName(templateDto.getName());
            template.setTemplateId(templateDto.getId());
            template.setSellerStoreId(sellerId);
            template.setIsDefault("true");
            templates.add(template);
        }
        return baseMapper.insertBatch(templates);
    }
}
